<?php
/**
 * 这里通用模型操作类
 *
 *
 *
 *@author hary <hary@taomee.com>
 */
class common_model extends model
{

	private $province_table;
	private $city_table;
	private $district_table;
	private $school_table;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		#$加载数据库使用表
		$this->province_table = $this->sys_conf_var['table']['province'];
		$this->city_table 	  = $this->sys_conf_var['table']['city'];
		$this->district_table = $this->sys_conf_var['table']['district'];
		$this->school_table   = $this->sys_conf_var['table']['school'];

		$this->loadClass('log');
	}

	/**
	 * 获取省份信息
	 *
	 * $id			 省份id
	 * $name		 name
	 * $islike       是否开启模糊匹配
	 * @return array 返回值类型 
	 */
	function getProvinces($id = '',$name='',$islike = 1)
	{
		$id   != ''?$this->where('`id`',$id,'=','and',0):null;

		if($name != '')
		{
			if($islike)
			{
				$name = preg_replace('/(省|自治州|盟|地区)/','',$name);
				$this->where('`name`',$name,'like');
			}
			else
			{
				$this->where('`name`',$name);
			}
		}

		return $this->getDataList($this->province_table);
	}
	/**
	 * 根据条件获取城市信息
	 *
	 * $pid 		  省份id
	 * $id			   城市id
	 * $name		 name
	 * @return array 返回值类型 
	 */
	function getCitys($pid = '',$id = '',$name = '',$islike = 1)
	{
		if($id != '')
		{
			$this->where('`id`',$id);
		}

		if($pid != '')
		{
			$this->where('`province_id`',$pid);
		}

		if($name != '')
		{
			if($islike)
			{
				$name = preg_replace('/(市|区|县|盟)/i','',$name);
				$this->where('`name`',$name,'like');
			}
			else
			{
				$this->where('`name`',$name);
			}
		}

		return $this->getDataList($this->city_table);
	}

	/**
	 * 根据条件获取区县信息
	 *
	 * $pid 		  省份id
	 * $cid			   城市id
	 * $id			   区县id
	 * @return array 返回值类型 
	 */
	function getDistricts($pid = '' ,$cid = '',$id = '',$name = '',$islike = 1)
	{
		if($pid != '')
		{
			$this->where('`province_id`',$pid);
		}

		if($cid != '')
		{
			$this->where('`city_id`',$cid);
		}

		if($id != '')
		{
			$this->where('`id`',$id);
		}

		if($name != '')
		{
				
			if($islike)
			{
				$this->where('`name`',$name,'like');
			}
			else
			{
				$this->where('`name`',$name);
			}
				
		}

		return $this->getDataList($this->district_table);
	}

	/**
	 * 根据条件获取学校信息
	 *
	 * $pid 		  省份id
	 * $cid			   城市id
	 * $did			   区县id
	 * $id			   学校id
	 * @return array 返回值类型 
	 */
	function getSchools($pid = '' ,$cid = '',$did = '',$id = '',$name = '',$islike = 1)
	{
		if($pid != '')
		{
			$this->where('`province`',$pid);
		}

		if($cid != '')
		{
			$this->where('`city`',$cid);
		}

		if($did != '')
		{
			$this->where('`district`',$did);
		}

		if($id != '')
		{
			$this->where('`id`',$id);
		}

		if($name != '')
		{
				
			if($islike)
			{
				$this->where('`name`',$name,'like');
			}
			else
			{
				$this->where('`name`',$name);
			}
				
		}

		return $this->getDataList($this->school_table);
	}

}